/**
 * Created by ${PRODUCT_NAME}.
 * User: skylerparr
 * Date: 12/12/10
 * Time: 8:52 AM
 * To change this template use File | Settings | File Templates.
 */
package com.thoughtorigin.util {
import com.thoughtorigin.module.ModuleLoader;
import com.thoughtorigin.util.mocksforinstantiator.SampleInst;

import org.flexunit.asserts.assertEquals;
import org.flexunit.asserts.assertNotNull;
import org.flexunit.asserts.assertTrue;
import org.flexunit.asserts.fail;
import org.mockito.integrations.any;
import org.mockito.integrations.between;
import org.mockito.integrations.given;
import org.mockito.integrations.mock;
import org.mockito.integrations.verify;


[RunWith("org.mockito.integrations.flexunit4.MockitoClassRunner")]
[Mock(type="com.thoughtorigin.module.ModuleLoader")]
public class InstantiatorTest {

    private var _moduleLoader: ModuleLoader;
    private var _classCreator: ClassCreator;

    [Before]
    public function setUp(): void {
        _classCreator = new ClassCreator();
        ObjectCreationError;
    }

    [After]
    public function tearDown(): void {
        _classCreator = null;
        _moduleLoader = null;
    }

    [Test]
    public function shouldCreateObjectWithUpTo9ConstructorArgs(): void {
        assertNotNull(ClassCreator.create(SampleInst, []));
        assertNotNull(ClassCreator.create(SampleInst, [""]));
        assertNotNull(ClassCreator.create(SampleInst, ["",""]));
        assertNotNull(ClassCreator.create(SampleInst, ["","",""]));
        assertNotNull(ClassCreator.create(SampleInst, ["","","",""]));
        assertNotNull(ClassCreator.create(SampleInst, ["","","","",""]));
        assertNotNull(ClassCreator.create(SampleInst, ["","","","","",""]));
        assertNotNull(ClassCreator.create(SampleInst, ["","","","","","",""]));
        assertNotNull(ClassCreator.create(SampleInst, ["","","","","","","",""]));
        assertNotNull(ClassCreator.create(SampleInst, ["","","","","","","","",""]));
    }

    [Test(expects="ReferenceError")]
    public function shouldOnlyAllowUpTo9Args(): void {
        assertNotNull(ClassCreator.create(SampleInst, ["","","","","","","","","",""]));
    }

    [Test]
    public function shouldCreateObjectIfArrayIsNull(): void {
        assertNotNull(ClassCreator.create(SampleInst, null));
    }

    [Test(expects="TypeError")]
    public function shouldThrowExceptionIfClassIsNull(): void {
        ClassCreator.create(null, null);
    }

    [Test]
    public function shouldGetClassFromModuleLoader(): void {
        _moduleLoader = mock(ModuleLoader);
        var obj: Class = Class;
        given(_moduleLoader.getClassByName(any())).willReturn(obj);

        _classCreator.moduleLoader = _moduleLoader;
        var obj2: Class = _classCreator.getClassByName("sample");
        assertEquals(obj, obj2);

        verify(between(1,1)).that(_moduleLoader.getClassByName);
    }

    [Test]
    public function shouldCreateInstanceFromName(): void {
        _moduleLoader = mock(ModuleLoader);
        var obj: Class = Object;
        given(_moduleLoader.getClassByName(any())).willReturn(obj);

        _classCreator.moduleLoader = _moduleLoader;
        var obj2: Object = _classCreator.createByClassName("sample", []);
        assertNotNull(obj2);
        assertTrue(obj2 is obj);

        verify(between(1,1)).that(_moduleLoader.getClassByName);
    }

    [Test(expects="com.thoughtorigin.util.ObjectCreationError")]
    public function shouldThrowExceptionIfClassIsNullFromModuleLoader(): void {
        _moduleLoader = mock(ModuleLoader);
        given(_moduleLoader.getClassByName(any())).willReturn(null);

        _classCreator.moduleLoader = _moduleLoader;
        var obj2: Object = _classCreator.createByClassName("sample", []);
    }

}
}
